package com.realestate.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.realestate.constants.AppConstant;
import com.realestate.model.BrtbUsers;
import com.realestate.services.UsersService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Controller
@RequestMapping("/account")
public class AccountController {
	
	private static final Logger logger = Logger.getLogger(AccountController.class);
	
	@Autowired
	private UsersService userServices;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView myAccount(@RequestParam(value="username", required=true) String username){
		ModelAndView result = new ModelAndView("account");
		BrtbUsers user = userServices.findUserByUsername(username);
		result.addObject("user", user);
		return result;
	}
	
	@RequestMapping(value = "/uploadImages", method = RequestMethod.POST)
	public ModelAndView uploadImagesPost(HttpServletRequest request) {
		request.getRealPath("images");
		Map<String, String[]> multiParameters = request.getParameterMap();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		if(multiParameters.get("username") != null){
			String username = multiParameters.get("username")[0];
			try {
				Map<String, MultipartFile> mapFiles = multipartRequest.getFileMap();
				Set<String> setStrings = mapFiles.keySet();
				String rootPath = System.getProperty("catalina.home");
				File dir = new File(rootPath + File.separator + AppConstant.APP_URL + File.separator + AppConstant.ACCOUNT);
				if(!dir.exists()){
					dir.mkdirs();
				}
				for(String set: setStrings){
					MultipartFile multipartField = mapFiles.get(set);
					File serverFile = new File(dir.getAbsolutePath() + File.separator + multipartField.getOriginalFilename());
					BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
					stream.write(multipartField.getBytes());
					stream.close();
					logger.info("Server File Location=" + serverFile.getAbsolutePath());
					userServices.saveAvatar(username, "/" + AppConstant.ACCOUNT + "/" + multipartField.getOriginalFilename());
				}
				return new ModelAndView(new RedirectView("/" + AppConstant.APP_URL + "/" + "account/?username=" + username));
			} catch (Exception e) {
				return new ModelAndView("500");
			}
		}
		return new ModelAndView("500");
	}
}
